package com.health.platform.provider.hospitalservices.service;

import com.health.platform.common.core.entity.BaseQueryEntity;
import com.health.platform.common.restful.AppResponse;
import com.health.platform.provider.homepage.dao.HomePageDao;

import com.health.platform.provider.homepage.entity.AvgCostEntity;
import org.springframework.stereotype.Service;
import com.health.platform.provider.hospitalservices.entity.HSSummaryEntity;
import com.health.platform.provider.hospitalservices.dao.HSDao;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.Resource;

/**
 * <p>医疗服务</p>
 * <p>创建日期：2021-03-02</p>
 */
@Service
public class HospitalServicesService {



    @Resource
    private HSDao hsDao;

    /**
     * 获取中间部分汇总信息
     * @param baseQueryEntity 参数
     * @return 结果
     */
    public AppResponse getSummaryInfo(BaseQueryEntity baseQueryEntity) throws NoSuchMethodException,
    IllegalAccessException, IllegalArgumentException,InvocationTargetException {
        HSSummaryEntity hsSummaryEntity=new HSSummaryEntity();


        baseQueryEntity.replaceKey("t.GBCODE","a.GBCODE");
        baseQueryEntity.replaceKey("t.bus_date","a.bus_date");


       // 获取门急诊、互联网人次
        HSSummaryEntity clinicEmeIntNum = hsDao.getOutpatientVisits(baseQueryEntity);
        // 获取门急诊、互联网人次
        HSSummaryEntity zyNum = hsDao.getHospitalVisits(baseQueryEntity);

        hsSummaryEntity.setDischarges(zyNum.getDischarges());
        hsSummaryEntity.setInDischarges(zyNum.getInDischarges());
        hsSummaryEntity.setHospitalVisits(zyNum.getHospitalVisits());
        hsSummaryEntity.setOutpatientVisits(clinicEmeIntNum.getOutpatientVisits());
        hsSummaryEntity.setEmergencyVisits(clinicEmeIntNum.getEmergencyVisits());
        hsSummaryEntity.setPeopleSurgery(String.valueOf(Integer.parseInt(clinicEmeIntNum.getPeopleSurgery())+Integer.parseInt(zyNum.getPeopleSurgery())));

        HSSummaryEntity hsSummaryEntityTb=new HSSummaryEntity();
        baseQueryEntity.replaceKey("a.bus_date","a.bus_date+365");
        // 获取门急诊、互联网人次
        HSSummaryEntity clinicEmeIntNumTb = hsDao.getOutpatientVisits(baseQueryEntity);
        // 获取门急诊、互联网人次
        HSSummaryEntity zyNumTb = hsDao.getHospitalVisits(baseQueryEntity);
        hsSummaryEntityTb.setDischarges(zyNumTb.getDischarges());
        hsSummaryEntityTb.setInDischarges(zyNumTb.getInDischarges());
        hsSummaryEntityTb.setHospitalVisits(zyNumTb.getHospitalVisits());
        hsSummaryEntityTb.setOutpatientVisits(clinicEmeIntNumTb.getOutpatientVisits());
        hsSummaryEntityTb.setEmergencyVisits(clinicEmeIntNumTb.getEmergencyVisits());
        hsSummaryEntityTb.setPeopleSurgery(String.valueOf(Integer.parseInt(clinicEmeIntNumTb.getPeopleSurgery())+Integer.parseInt(zyNumTb.getPeopleSurgery())));
        Map<String, Object> result = new HashMap<>(2);
        if (hsSummaryEntityTb!=null){
            // 获取实体类的所有属性，返回Field数组
            Field[] field = hsSummaryEntityTb.getClass().getDeclaredFields();
            for (int j = 0; j < field.length; j++) {
                // 获取属性的名字
                String name = field[j].getName();
                // 将属性的首字符大写，方便构造get，set方法
                name = name.substring(0, 1).toUpperCase() + name.substring(1);
                // 获取属性的类型
                String type = field[j].getGenericType().toString();
                if (type.equals("class java.lang.String")) {
                    Method m = hsSummaryEntityTb.getClass().getMethod("get" + name);
                    // 调用getter方法获取属性值
                    String value = (String) m.invoke(hsSummaryEntityTb);
                    if (value != null && !"0".equals(value)) {
                        //门诊人次
                        if("OutpatientVisits".equals(name)){
                            result.put("outpatientVisitsHb",(Integer.parseInt(hsSummaryEntity.getOutpatientVisits())-Integer.parseInt(value))/Integer.parseInt(value));
                        }else if("EmergencyVisits".equals(name)){
                            // 急诊人次
                            result.put("emergencyVisitsHb",(Integer.parseInt(hsSummaryEntity.getEmergencyVisits())-Integer.parseInt(value))/Integer.parseInt(value));
                        }else if("HospitalVisits".equals(name)){
                            // 入院人次
                            result.put("hospitalVisitsHb",(Integer.parseInt(hsSummaryEntity.getHospitalVisits())-Integer.parseInt(value))/Integer.parseInt(value));
                        }else if("Discharges".equals(name)){
                            // 出院人次
                            result.put("dischargesHb",(Integer.parseInt(hsSummaryEntity.getDischarges())-Integer.parseInt(value))/Integer.parseInt(value));
                        }else if("InDischarges".equals(name)){
                            // 在院人次
                            result.put("inDischargesHb",(Integer.parseInt(hsSummaryEntity.getInDischarges())-Integer.parseInt(value))/Integer.parseInt(value));
                        }else if("PeopleSurgery".equals(name)){
                            // 手术人次
                            result.put("peopleSurgeryHb",(Integer.parseInt(hsSummaryEntity.getPeopleSurgery())-Integer.parseInt(value))/Integer.parseInt(value));
                        }


                    } else {
                        //门诊人次
                        if("OutpatientVisits".equals(name)){
                            result.put("outpatientVisitsHb","0");
                        }else if("EmergencyVisits".equals(name)){
                            // 急诊人次
                            result.put("emergencyVisitsHb","0");
                        }else if("HospitalVisits".equals(name)){
                            // 入院人次
                            result.put("hospitalVisitsHb","0");
                        }else if("Discharges".equals(name)){
                            // 出院人次
                            result.put("dischargesHb","0");
                        }else if("InDischarges".equals(name)){
                            // 在院人次
                            result.put("inDischargesHb","0");
                        }else if("PeopleSurgery".equals(name)){
                            // 手术人次
                            result.put("peopleSurgeryHb","0");
                        }
                    }
                }
            }

        }
        result.put("hsSummaryEntity",hsSummaryEntity);
        return AppResponse.success("查询成功", result);
    }


    /**
     * 获取就诊人次变化情况
     * @param baseQueryEntity 参数
     * @return 结果
     */
    public AppResponse getSeeDoctor(BaseQueryEntity baseQueryEntity){
        Map<String, Object> result = new HashMap<>();
        AvgCostEntity avgCostEntity=new AvgCostEntity();
        // 获取门诊数据就诊人次
        List<AvgCostEntity> oet=   hsDao.getOutEmeTre(baseQueryEntity);
        //近12个月的就诊人次
        List<AvgCostEntity> currentYearRateResult= new ArrayList<>();
        // 同期12个月就诊人次
        List<AvgCostEntity> lastYearRateResult= new ArrayList<>();
        avgCostEntity.setKeyMonth("2020/04");
        avgCostEntity.setTotal(12.f);
        avgCostEntity.setBusDate("2020/04");
        oet.add(avgCostEntity);
        avgCostEntity=new AvgCostEntity();
        avgCostEntity.setKeyMonth("2020/03");
        avgCostEntity.setTotal(12.f);
        avgCostEntity.setBusDate("2020/04");
        oet.add(avgCostEntity);
        List<AvgCostEntity> oetTb=new ArrayList<>();
        setRate(currentYearRateResult,lastYearRateResult,oet);
        // 门急诊同比
        oetTb= changeTb(currentYearRateResult,lastYearRateResult);
        List<AvgCostEntity> oetHb=new ArrayList<>();
        // 门急诊环比
        oetHb=changeHb(currentYearRateResult,lastYearRateResult);
        result.put("oetTb",oetTb);
        result.put("oetHb",oetHb);


        // 获取门诊数据就诊人次
        List<AvgCostEntity> oH=   hsDao.getOutOfHos(baseQueryEntity);
        //近12个月的出院人次
        List<AvgCostEntity> currentYearOHResult= new ArrayList<>();
        // 同期12个月出院人次
        List<AvgCostEntity> lastYearOHResult= new ArrayList<>();
        List<AvgCostEntity> ohTb=new ArrayList<>();
        setRate(currentYearOHResult,lastYearOHResult,oH);
        // 门急诊同比
        ohTb= changeTb(currentYearOHResult,lastYearOHResult);
        List<AvgCostEntity> ohHb=new ArrayList<>();
        // 门急诊环比
        ohHb=changeHb(currentYearOHResult,lastYearOHResult);
        result.put("ohTb",ohTb);
        result.put("ohHb",ohHb);

        //近12个月的手术人次
        List<AvgCostEntity> surgery=   hsDao.getSurgery(baseQueryEntity);
        //近12个月的手术人次
        List<AvgCostEntity> currentYearSurgeryResult= new ArrayList<>();
        // 同期12个月手术人次
        List<AvgCostEntity> lastYearSurgeryResult= new ArrayList<>();
        List<AvgCostEntity> surgeryTb=new ArrayList<>();
        setRate(currentYearSurgeryResult,lastYearSurgeryResult,surgery);
        // 门急诊同比
        surgeryTb= changeTb(currentYearOHResult,lastYearOHResult);
        List<AvgCostEntity> surgeryHb=new ArrayList<>();
        // 门急诊环比
        surgeryHb=changeHb(currentYearOHResult,lastYearOHResult);
        result.put("surgeryTb",surgeryTb);
        result.put("surgeryHb",surgeryHb);

        return AppResponse.success("查询成功", result);
    }

    /**
     * 出院患者平均住院趋势
     * @param baseQueryEntity
     * @return
     */
    public AppResponse getHd(BaseQueryEntity baseQueryEntity){
        List<AvgCostEntity>  hd= hsDao.getHosDay(baseQueryEntity);
        return AppResponse.success("查询成功", hd);
    }


    /**
     * 计算
     */
    private void setRate(List<AvgCostEntity> currentYearRateResult, List<AvgCostEntity> lastYearRateResult, List<AvgCostEntity> utilizationRateList){
        boolean addFlag = true;
        for(AvgCostEntity avgCostEntity : utilizationRateList){
            if(addFlag){
                currentYearRateResult.add(avgCostEntity);
                if(avgCostEntity.getBusDate().equals(avgCostEntity.getKeyMonth())){
                    addFlag = false;
                }
            } else {
                lastYearRateResult.add(avgCostEntity);
            }
        }
    }
    // 获取环比信息月份
    public String lastHb(Integer month){
        if(month<10){
            return "0"+month;
        }

        return ""+month;
    }

    // 环比
    public List<AvgCostEntity> changeHb(List<AvgCostEntity> currentYearRateResult, List<AvgCostEntity> lastYearRateResult){
        Float drugsTotal = 0.0F;
        AvgCostEntity avgCostEntity=new AvgCostEntity();
        List<AvgCostEntity> tq=new ArrayList<>();
        //环比
        if(currentYearRateResult!=null && currentYearRateResult.size()>0){
            for (AvgCostEntity curr : currentYearRateResult) {
                for (AvgCostEntity currLast : currentYearRateResult) {
                    String[] currarr = currLast.getBusDate().split("/");
                    // 获取上月
                    String lm=currarr[1]+lastHb(Integer.parseInt(currarr[1])+1);
                    if(curr.getBusDate().equals(lm)){
                        if(currLast.getTotal()!=null && currLast.getTotal()!=0){
                            drugsTotal=(curr.getTotal()-currLast.getTotal())/currLast.getTotal();
                        }
                        avgCostEntity=new AvgCostEntity();
                        avgCostEntity.setBusDate(curr.getBusDate());
                        avgCostEntity.setTotal(drugsTotal);
                        tq.add(avgCostEntity);
                        drugsTotal = 0.0F;
                    }

                }
                if(curr.getBusDate().equals(curr.getKeyMonth())){
                    if(lastYearRateResult!=null && lastYearRateResult.size()>0){
                        if(lastYearRateResult.get(0).getTotal()!=null && lastYearRateResult.get(0).getTotal()!=0){
                            drugsTotal=(curr.getTotal()-lastYearRateResult.get(0).getTotal())/lastYearRateResult.get(0).getTotal();
                        }
                        avgCostEntity=new AvgCostEntity();
                        avgCostEntity.setBusDate(curr.getBusDate());
                        avgCostEntity.setTotal(drugsTotal);
                        tq.add(avgCostEntity);
                        drugsTotal = 0.0F;
                    }
                }
            }

        }
        return tq;
    }

    // 同比T
    public List<AvgCostEntity> changeTb(List<AvgCostEntity> currentYearRateResult, List<AvgCostEntity> lastYearRateResult){
        Float drugsTotal = 0.0F;
        AvgCostEntity avgCostEntity=new AvgCostEntity();
        List<AvgCostEntity> tq=new ArrayList<>();
        //同比
        if(lastYearRateResult!=null && lastYearRateResult.size()>0){
            for(AvgCostEntity last: lastYearRateResult) {
                for (AvgCostEntity curr : currentYearRateResult) {
                    String[] lastarr = last.getBusDate().split("/");
                    String[] currarr = curr.getBusDate().split("/");
                    System.out.println(Integer.parseInt(currarr[1])-1);
                    //同期
                    if(lastarr[1].equals(currarr[1])){
                        if(last.getTotal()!=null && last.getTotal()!=0){
                            drugsTotal=(curr.getTotal()-last.getTotal())/last.getTotal();
                        }
                        avgCostEntity=new AvgCostEntity();
                        avgCostEntity.setBusDate(curr.getBusDate());
                        avgCostEntity.setTotal(drugsTotal);
                        tq.add(avgCostEntity);
                        drugsTotal = 0.0F;
                    }
                }
            }
        }else{
            for (AvgCostEntity curr : currentYearRateResult) {
                avgCostEntity=new AvgCostEntity();
                avgCostEntity.setBusDate(curr.getBusDate());
                avgCostEntity.setTotal(drugsTotal);
                tq.add(avgCostEntity);
                drugsTotal = 0.0F;
            }
        }
        return tq;
    }
}
